package com.example.demozy.mapper;

import com.example.demozy.entity.Challenge;
import com.example.demozy.entity.Participant;
import org.apache.ibatis.annotations.*;

import java.sql.Timestamp;
import java.util.List;

/**
 * 挑战赛mapper
 *
 * @author 王成龙
 */
@Mapper
public interface ChallengeMapper {

    /**
     * 新增挑战赛记录
     *
     * @param challengeName
     * @param startTime
     * @param endTime
     * @param stockCode
     * @param startingFunds
     * @param maxParticipantsNumber
     * @return
     */
    @Insert("insert into challenge " +
            "values(0,#{challengeName},#{introduction},#{startTime}," +
            "#{endTime},#{stockCode}," +
            "#{startingFunds},#{maxParticipantsNumber},0," + "0,now())")
    int insertChallenge(@Param("challengeName") String challengeName,
            @Param("introduction") String introduction,
            @Param("startTime") Timestamp startTime,
            @Param("endTime") Timestamp endTime,
            @Param("stockCode") String stockCode,
            @Param("startingFunds") double startingFunds,
            @Param("maxParticipantsNumber") int maxParticipantsNumber);

    /**
     * 根据挑战赛id返回挑战赛
     *
     * @param challengeId
     * @return
     */
    @Select("select * " + "from challenge " +
            "where challenge_id = #{challengeId}")
    Challenge getChallengeById(int challengeId);

    /**
     * 得到最新的挑战赛id
     *
     * @return
     */
    @Select("select max(challenge_id) " + "from challenge")
    int getMaxChallengeId();

    /**
     * 获取所有正在进行的挑战赛即challenge_stat = 0的挑战赛
     *
     * @return
     */
    @Select("select * " + "from challenge " +
            "where challenge_stat = 0 order by create_time desc")
    List<Challenge> listChallengesInProgress();

    /**
     * 获取所有已结束的挑战赛即challenge_stat = 1的挑战赛
     *
     * @return
     */
    @Select("select * " + "from challenge " +
            "where challenge_stat = 1 order by create_time desc")
    List<Challenge> listChallengesEnded();

    /**
     * 向参数用户表插入数据
     *
     * @param challengeId
     * @param userId
     * @param conditionSheetId
     * @param startingFunds
     * @param currentFunds
     * @return
     */
    @Insert("insert into participant " +
            "values(0,#{challengeId},#{userId},0,#{startingFunds}," +
            "#{currentFunds},0,#{conditionSheetId},now())")
    int insertParticipant(@Param("challengeId") int challengeId,
            @Param("userId") int userId,
            @Param("conditionSheetId") String conditionSheetId,
            @Param("startingFunds") double startingFunds,
            @Param("currentFunds") double currentFunds);

    /**
     * 对应id的挑战赛的参赛人数+1
     *
     * @param challengeId
     * @return
     */
    @Update("update challenge " +
            "set participants_number = participants_number + 1 " +
            "where challenge_id = #{challengeId}")
    int updateChallengeById(int challengeId);

    /**
     * 根据挑战赛id获取当前报名参赛人数
     *
     * @param challengeId
     * @return
     */
    @Select("select participants_number " + "from challenge " +
            "where challenge_id = #{challengeId}")
    int getParticipantsNumberById(int challengeId);

    /**
     * 根据挑战赛id获取最大参数人数
     *
     * @param challengeId
     * @return
     */
    @Select("select max_participants_number " + "from challenge " +
            "where challenge_id = #{challengeId}")
    int getMaxParticipantsNumberById(int challengeId);

    /**
     * 根据挑战赛id获取所有参赛用户的列表
     *
     * @param challengeId
     * @return
     */
    @Select("select * " + "from participant " +
            "where challenge_id = #{challengeId}")
    List<Participant> listParticipantsById(
            @Param("challengeId") int challengeId);

    /**
     * 根据用户id获取挑战赛id列表
     *
     * @param userId
     * @return
     */
    @Select("select challenge_id from participant where user_id = #{userId} " +
            "order by update_time desc")
    List<Integer> listChallengeIdByUserId(@Param("userId") int userId);

    /**
     * 根据挑战赛id和状态返回挑战赛
     *
     * @param challengeId
     * @return
     */
    @Select("select * " + "from challenge " +
            "where challenge_id = #{challengeId} and challenge_stat = " +
            "#{challengeStat}")
    Challenge getChallengeUserById(@Param("challengeId") int challengeId,
            @Param("challengeStat") int challengeStat);

    /**
     * 将超时的挑战赛状态设置为1，即已结束
     *
     * @return
     */
    @Update("update challenge set challenge_stat = 1 where end_time <= now()")
    int updateChallengeByEndTime();

    /**
     * 结束挑战赛的参赛用户的参赛状态设为1，即已结束
     *
     * @return
     */
    @Update("update participant join challenge " +
            "on (challenge.challenge_id = participant.challenge_id and " +
            "challenge.challenge_stat = 0) " + "set participant.entry_stat = 1")
    int updateEntryStat();

    /**
     * 根据挑战赛id、用户id获取参赛的条件单id
     * @param challengeId
     * @param userId
     * @return
     */
    @Select("select condition_sheet_id from participant where challenge_id = " +
            "#{challengeId} and user_id = #{userId}")
    String getConditionSheetIdByIds(@Param("challengeId") int challengeId,
            @Param("userId") int userId);

    /**
     * 更新参赛用户的收益率
     * @param challengeId
     * @param userId
     * @param yield
     * @return
     */
    @Update("update participant set yield = #{yield} where challenge_id = #{challengeId} and user_id = #{userId}")
    int updateYield(@Param("challengeId") int challengeId,
            @Param("userId") int userId,@Param("yield") double yield);
}
